﻿//Write a program that can solve these tasks:
//  1.Reverses the digits of a number
//  2.Calculates the average of a sequence of integers
//  3.Solves a linear equation a * x + b = 0
//Create appropriate methods.
//Provide a simple text-based menu for the user to choose which task to solve.
//Validate the input data:
//  1.The decimal number should be non-negative
//  2.The sequence should not be empty
//  3.a should not be equal to 0


using System;
using System.Numerics;

public class SolveTasks
{
    public static void Main()
    {
        int choice = Menu();
        BigInteger reversedNumber = 0;
        switch (choice)
        {
            case 1:
                {
                    string number = UserDataInput(1);
                    BigInteger validNumber = ValidateData(number);
                    if (validNumber == -1)
                    {
                        Console.Clear();
                        Console.WriteLine("Error!!!The input is NOT number or it is less than zero!!!" +
                                            "\nTry again");
                        Main();
                    }
                    reversedNumber = Reverse(validNumber);
                    Console.WriteLine("Reversed number is {0}", reversedNumber);
                }
                break;
            case 2:
                {
                    string userInput = UserDataInput(2);
                    bool isValid = true;
                    int[] array = ValidateData(userInput, ref isValid);
                    if (!isValid)
                    {
                        Console.Clear();
                        Console.WriteLine("The data you are input is NOT valid!!! Try again!!!");
                        Main();
                    }
                    decimal result = CalculatesAverage(array);
                    Console.WriteLine("Average of a sequence of integers is {0}", result);
                }
                break;
            case 3:
                {
                    string userInput = UserDataInput(3);
                    bool isValid = true;
                    int[] array = ValidateData(userInput, ref isValid);
                    if (!isValid)
                    {
                        Console.Clear();
                        Console.WriteLine("Parameter 'a' must NOT be zero or the data you are input is NOT valid!!! Try again!!!");
                        Main();
                    }
                    decimal result = SolveEquation(array[0], array[1]);
                    Console.WriteLine("The solution of equation is {0}", result);
                }
                break;
            default:
                {
                    Console.Clear();
                    Console.WriteLine("Bad choice!Try again!");
                    Main();
                }
                break;
        }
    }

    public static int Menu()
    {
        Console.WriteLine("Choose which task to solve:");
        Console.Write("   1.Reverses the digits of a number" + 
                    "\n   2.Calculates the average of a sequence of integers" +
                    "\n   3.Solves a linear equation a * x + b = 0");
        Console.Write("\nEnter number in range[1..3] Choice = ");
        int choice = int.Parse(Console.ReadLine());
        return choice;
    }

    public static string UserDataInput(int choice)
    {
        switch (choice)
        {
            case 1:
                {
                    Console.Write("\nEnter integer number great than zero Number = ");
                    string userInput = Console.ReadLine();
                    return userInput;
                }
            case 2:
                {
                    Console.WriteLine("\nEnter sequence of integers separated by commas");
                    string userInput = Console.ReadLine();
                    return userInput;
                }
            case 3:
                {
                    Console.Write("\nEnter number for parameters of linear equation a * x + b = 0" +
                                   "\n a = ");
                    string userInput = Console.ReadLine();
                    Console.Write("\n b = ");
                    userInput = userInput + ',' + Console.ReadLine();
                    return userInput;
                }
        }
        return "";
    }

    private static BigInteger ValidateData(string data)
    {
        BigInteger bigNumber = new BigInteger();
        bool isValid = BigInteger.TryParse(data, out bigNumber);
        if (!isValid || bigNumber < 0)
        {
            return -1;
        }
        return bigNumber;
    }

    private static int[] ValidateData(string data, ref bool isValidInput)
    {
        string[] str = data.Split(',');
        int[] array = new int[str.Length];
        for (int i = 0; i < array.Length; i++)
        {
            bool isValid = int.TryParse(str[i], out array[i]);
            if (!isValid || array[i] == 0)
            {
                isValidInput = false;
                Array.Clear(array, 0, array.Length);
                return array;
            }
        }
      
        return array;
    }

    private static BigInteger Reverse(BigInteger number)
    {
        int count = 0;
        BigInteger integer = number;
        do
        {
            number /= 10;
            count++;
        } while (number > 0);

        for (int i = count - 1; i >= 0; i--)
        {
            BigInteger temp = integer % 10;
            number += temp * (BigInteger)Math.Pow(10, i);
            integer /= 10;
        }

        return number;
    }

    public static decimal CalculatesAverage(int[] array)
    {
        decimal result = 0;
        foreach (var item in array)
        {
            result += item;
        }
        result = result / array.Length;
        return result;
    }

    public static decimal SolveEquation(decimal a, decimal b)
    {
        return -(b / a);
    }
}